# -*- mode:sh -*-

# get the latest list of wnpp bugs and their source packages
function wnppbugs() {
    log "Fetching latest list of wnpp bugs"
    TMPLIST=$( gettempfile )
    wget -q -O${TMPLIST} --ca-directory=/etc/ssl/ca-debian https://qa.debian.org/data/bts/wnpp_rm
    chmod go+r ${TMPLIST}
    mv ${TMPLIST} ${scriptdir}/masterfiles/wnpp_rm
}

# Push files over to security
function pushfilesdb() {
    log "Pushing files table to security"
    # The key over there should have the following set for the ssh key:
    #  command="/srv/security-master.debian.org/dak/scripts/debian/update-external-files --update"
    psql -c 'COPY files (id, filename, size, md5sum, last_used, sha1sum, sha256sum, created, modified) TO STDOUT' projectb | \
        xz -3 | \
        ssh -o BatchMode=yes -o ConnectTimeout=30 -o SetupTimeout=30 -2 \
            -i ${base}/s3kr1t/push_external_files dak@security-master.debian.org sync
}

# Update wanna-build dump
function wbdump() {
    log "Update wanna-build database dump"
    ${masterdir}/scripts/nfu/get-w-b-db
}

# Write out sections file
function writesections() {
    log "Writing sections.822"
    dak write-sections > ${exportdir}/sections.822
}

# Generate stats about the new queue
function newstats() {
    log "Generating stats about the new queue"
    dak stats new ${webdir}/NEW-stats.yaml 2> /dev/null
}

# Generate the contributor data
function contributor() {
    log "Submitting data to contributors"
    TMPCNTB=$( gettempfile )
    REQUESTS_CA_BUNDLE=/etc/ssl/ca-debian/ca-certificates.crt dc-tool --mine="${configdir}/contributor.source" --auth-token @"${base}/s3kr1t/contributor.auth" --source ftp.debian.org --json > ${TMPCNTB}

    # Post with curl as a workaround for #801506
    # See https://wiki.debian.org/ServicesSSL#curl
    dir=/etc/ssl/ca-debian
    test -d ${dir} && capath="--capath ${dir}"
    curl -s ${capath} https://contributors.debian.org/contributors/post \
         -F source=ftp.debian.org \
         -F auth_token="$(cat ${base}/s3kr1t/contributor.auth)" \
         -F data=@${TMPCNTB} > ${TMPCNTB}.result
    log "Results:"
    cat ${TMPCNTB}.result
    log "----"
    rm -f ${TMPCNTB}.result
}

# Clean up urgency log
function cleanurgencylog() {
    log "Cleaning up urgency log"
    find /srv/ftp.debian.org/web/britney/urgencies -type f -mtime +15 -delete
}

# Update keyring for buildd-add-keys, buildd-remove-keys:
function update-buildd-admin-keyring() {
    log "Updateing buildd admin keyring"
    . ${configdir}/buildd-keyrings.conf
    cd ${scriptdir}/builddkeyrings
    gpg --no-tty --no-default-keyring --keyring /srv/keyring.debian.org/keyrings/debian-keyring.gpg --export "${buildd_admin_keys[@]}" |
        gpg --no-tty --no-default-keyring --keyring ./adminkeys.gpg.new --import
    mv adminkeys.gpg.new adminkeys.gpg
}
